package com.ble.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.InputType;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.ble.BleDetailActivity;
import com.ble.R;
import com.ble.base.BaseFragment;
import com.ble.utlis.CmdUtlis;
import com.ble.utlis.LineChartHandle;
import com.ble.utlis.TextTools;
import com.ble.utlis.ToastUtil;
import com.ble.weight.MyAlertDialog;
import com.github.mikephil.charting.charts.LineChart;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.BiConsumer;

public class WaveFormFragment extends BaseFragment implements View.OnClickListener {


    private LineChart lc_hyjcqx, lc_hyqxpp;
    private LineChartHandle lineChartHandle, lineChartHandlexpp;
    private TextView tv_40004, tv_40023;
    private SeekBar sb_40023;
    private SeekBar sb_40004;
    private BleDetailActivity bleDetailActivity;
    private TextView tv_update;

    @Override
    protected int getContentResid() {
        return R.layout.fragment_wave;
    }


    int pointNum41001 = 0;
    String TAG = "TAG111";

    List<Float> points41001 = new ArrayList<>();
    List<Float> points42001 = new ArrayList<>();

    public void add41001(List<Float> points,boolean flag){
        if(flag){
            points41001.clear();
        }
        points41001.addAll(points);


    }

    SimpleDateFormat sff=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public void add42001(List<Float> points,boolean flag){

        if(flag){
            points42001.clear();
        }
        points42001.addAll(points);

            lineChartHandlexpp.clear();
            lineChartHandlexpp.addLine1Data(points42001);


            lineChartHandle.clear();
            lineChartHandle.addLine1Data(points41001);

        tv_update.setText("刷新时间"+sff.format(new Date()));

    }
    int pointNum42001 = 0;




    Map<Integer, TextView> map = new HashMap<>();

    @Override
    protected void init(View view) {
        lc_hyjcqx = view.findViewById(R.id.lc_hyjcqx);
        lineChartHandle = new LineChartHandle();
        lineChartHandle.setBl(4);
        lineChartHandle.setYBl(2.55f);
        lineChartHandle.setMax(255);
        lineChartHandle.setColor(Color.parseColor("#0c764b"));
        lineChartHandle.initLineChart(lc_hyjcqx);

        bleDetailActivity = (BleDetailActivity) self;

//
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                List<Float> points = new ArrayList<>();
//                for (int i = 0; i < 50; i++) {
//                    Float random = lineChartHandle.getRandom(300f);
//                    points.add(random);
//                }
//                self.runOnUiThread(() -> add42001(points,false));
//
//            }
//        },1300,300);


        lc_hyqxpp = view.findViewById(R.id.lc_hyqxpp);
        lineChartHandlexpp = new LineChartHandle();
        lineChartHandlexpp.setColor(Color.parseColor("#ea403e"));
        lineChartHandlexpp.initLineChart(lc_hyqxpp);


        sb_40004 = view.findViewById(R.id.sb_40004);
        tv_40004 = view.findViewById(R.id.tv_40004);
        sb_40004.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                tv_40004.setText(i + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                cmd(" 灵敏度设置", (byte) 0x06,3, 0, 1000, sb_40004.getProgress()+"");
            }
        });
        sb_40023 = view.findViewById(R.id.sb_40023);
        tv_40023 = view.findViewById(R.id.tv_40023);
        tv_update = view.findViewById(R.id.tv_update);

        sb_40023.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                tv_40023.setText(i + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                cmd("采样最小值", (byte) 0x06,23, 0, 2000, sb_40023.getProgress()+"");
            }
        });


        TextView tv_40021 = view.findViewById(R.id.tv_40021);
        TextView tv_40022 = view.findViewById(R.id.tv_40022);


        TextView tv_40007 = view.findViewById(R.id.tv_40007);
        TextView tv_40008 = view.findViewById(R.id.tv_40008);
        TextView tv_40010 = view.findViewById(R.id.tv_40010);
        TextView tv_40011 = view.findViewById(R.id.tv_40011);

        map.put(40021, tv_40021);
        map.put(40022, tv_40022);
        map.put(40007, tv_40007);
        map.put(40008, tv_40008);
        map.put(40010, tv_40010);
        map.put(40011, tv_40011);

        map.put(40004, tv_40004);
        map.put(40023, tv_40023);


        tv_40007.setOnClickListener(this);
        tv_40008.setOnClickListener(this);
        tv_40010.setOnClickListener(this);
        tv_40011.setOnClickListener(this);
    }

    public void setValue(int adress, String value, byte[] b_value) {

        SpannableString content = new SpannableString(value);
        try {
            TextView textView = map.get(adress);

            if (adress == 40007) {
                content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            } else if (adress == 40008) {
                content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            } else if (adress == 40010) {
                content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            } else if (adress == 40011) {
                content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            } else if (adress == 40004) {
                sb_40004.setProgress(Integer.valueOf(value));
            } else if (adress == 40023) {
                sb_40023.setProgress(Integer.valueOf(value));
            }
            textView.setText(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        TextView tv= (TextView) view;
        String text = tv.getText().toString();


        switch (view.getId()) {
            case R.id.tv_40007:
                cmd("辨别最高频率", (byte) 0x06,7, 1, 500,text);
                break;
            case R.id.tv_40008:
                cmd("辨别最低频率", (byte) 0x06, 8, 1, 500,text);
                break;
            case R.id.tv_40010:
                cmd("判别能量拉入值", (byte) 0x06,10, 1, 99,text);
                break;
            case R.id.tv_40011:
                cmd("判别能量脱开值",(byte) 0x06, 11, 1, 99,text);
                break;
        }
    }

    private void cmd(String title, byte code, int address, int min, int max,String text) {
        EditText editText = new EditText(self);
        editText.setText(text);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        AlertDialog.Builder builder = new AlertDialog.Builder(self);
        builder.setTitle(title+"  范围"+min+"~"+max)
                .setView(editText)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", (dialogInterface, i1) -> {

                    String trim = editText.getText().toString().trim();
                    Integer integer = Integer.valueOf(trim);
                    if(min>integer||max<integer){
                        ToastUtil.showShort(self, title+"设置范围为"+min+"到"+max);
                        return;
                    }
                    byte[] bytes = CmdUtlis.grtCmdSetting2(code, address, TextTools.intToByte2(Integer.valueOf(trim)));
                    bleDetailActivity.sendCmd(bytes);
                }).show();
    }

}
